package cn.hxzy.common.response;

import lombok.Data;

/**
 * 创建时间:2024-04-17 09:51:38 星期三
 * 统一返回结果
 */
@Data
public class R<T> {
    /**
     * 接口响应状态码
     */
    private Integer code;

    /**
     * 接口响应信息
     */
    private String msg;

    /**
     * 接口响应的数据
     */
    private T data;

    //成功的方法
    public static <T> R<T> success(T data) {
        R<T> response = new R<>();
        response.setData(data);
        response.setCode(StatusEnum.SUCCESS.code);
        response.setMsg(StatusEnum.SUCCESS.message);
        return response;
    }

    //失败的方法
    public static <T> R<T> error(StatusEnum statusEnum) {
        R<T> response = new R<>();
        response.setCode(statusEnum.code);
        response.setMsg(statusEnum.message);
        return response;
    }

    public static <T> R<T> error(int code, String message) {
        R<T> response = new R<>();
        response.setCode(code);
        response.setMsg(message);
        return response;
    }

    public static <T> R<T> error(int code, String message, T data) {
        R<T> response = new R<>();
        response.setCode(code);
        response.setMsg(message);
        response.setData(data);
        return response;
    }
}
